package com.darrenchan.leetcode;

/**
 * @Desc
 * @Author chenchi03
 * @CreateTime 2020-04-24 21:43
 */
public class QLint148 {
    public void sortColors(int[] nums) {
        int index = sort(nums, 0, nums.length - 1, 0);
        sort(nums, index, nums.length - 1, 1);
    }

    private int sort(int[] nums, int i, int j, int target) {
        while(i <= j) {
            while (i <= j && nums[i] == target) {
                i++;
            }

            while (i <= j && nums[j] > target) {
                j--;
            }

            if(i <= j) {
                int temp = nums[i];
                nums[i] = nums[j];
                nums[j] = temp;

                i++;
                j--;
            }
        }

        return i;
    }
}
